package com.wly.dbc
{
	import com.wly.data.ItemVO;
	import com.wly.data.TypeVO;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	public class XmlWriter
	{
		static public function writeItemsList(fileName:String, items:Array):void
		{
			var i:int;
			var j:int;
			var item:ItemVO;
			var itemXml:XML;
			var typeXml:XML;
			
			var gameContentXml:XML = 
				<items>
				</items>;
			
			if(!items)
			{
				return;
			}
			for(i=0; i<items.length; i++)
			{
				item = items[i] as ItemVO;
				if(item)
				{
					itemXml = <item>
						     </item>;
					itemXml.@id  = item.id
					itemXml.@url = item.url;
					itemXml.@width = item.width;
					itemXml.@height = item.height;
					
					for(j=0; j<item.types.length; j++)
					{
						typeXml = <type />
						typeXml.@id = item.types[j];
						itemXml.appendChild(typeXml);
					}
					gameContentXml.appendChild(itemXml);
				}
				
			}
			
			var file:File = new File(File.applicationDirectory.resolvePath(fileName).nativePath);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(gameContentXml.toString());
			fs.close();
		}
		
		static public function writeItemsTypes(fileName:String, types:Array):void
		{
			var i:int;
			var type:TypeVO;
			var typeXml:XML;
			var typeItemXml:XML;
			
			if(!types)
			{
				return;
			}
			
			typeXml = <types>
					  </types>;
			
			for(i=0; i<types.length; i++)
			{
				type = types[i] as TypeVO;
				if(!type)
				{
					continue;
				}
				
				typeItemXml = <type />;
				typeItemXml.@id   = type.id;
				typeItemXml.@desc = type.desc;
				typeXml.appendChild(typeItemXml);
			}
			
			var file:File = new File(File.applicationDirectory.resolvePath(fileName).nativePath);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(typeXml.toString());
			fs.close();
		}
	}
}